package ssi.staaar.utility.camera;

import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class StaaarCameraView extends SurfaceView {
	Camera camera;
	SurfaceHolder previewHolder;
	
	public StaaarCameraView(Context context) {
		super(context);
		previewHolder = this.getHolder();
		previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	    SurfaceHolder.Callback surfaceHolderListener = 
	    	new SurfaceHolder.Callback() {
	    	
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				camera.stopPreview();
				camera.release();
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				try {
					camera = Camera.open();
					camera.setPreviewDisplay(previewHolder);
				} catch (IOException e) {
					e.printStackTrace();
				}	
			}
		
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, 
					int width, int height) {
				Parameters params = camera.getParameters();
				params.setPreviewSize(800, 600);
				params.setPictureFormat(PixelFormat.JPEG);
				camera.startPreview();
			}
	    };
	    previewHolder.addCallback(surfaceHolderListener);
	}

	public void stopCamera() {
		camera.release();
	}
	
	public void onDraw(Canvas c) {
		super.onDraw(c);
	}
}